package com.itboye.eweilisten.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.SchoolBean;
import com.itboye.eweilisten.util.widget.TimeFormat;

import java.util.List;

/**
 * Created by itboye on 2017/4/10.
 */

public class WrongAdapter extends BaseAdapter {

    Context context;
    List<SchoolBean.InfoList> list;

    public WrongAdapter(Context context, List<SchoolBean.InfoList> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size() > 30 ? 30 : list.size();//最多只显示30条
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHouder houder;
        if (convertView == null) {
            houder = new ViewHouder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_chengji, null);
            houder.tvScollTime = (TextView) convertView.findViewById(R.id.tvScollTime);
            houder.tvScollCount = (TextView) convertView.findViewById(R.id.tvScollCount);
            houder.tvScollYes = (TextView) convertView.findViewById(R.id.tvScollYes);
            houder.tvScollWrong = (TextView) convertView.findViewById(R.id.tvScollWrong);
            houder.tvScollLeak = (TextView) convertView.findViewById(R.id.tvScollLeak);
            houder.tvScollData = (TextView) convertView.findViewById(R.id.tvScollData);
            convertView.setTag(houder);
        } else {
            houder = (ViewHouder) convertView.getTag();
        }

        int yes;
        int Wrong;
        int Leak;
        long time = 0;
        try {
            if (list.get(position).getUse_time() == null || list.get(position).getUse_time().equals("")) {

            } else
                time = Long.parseLong(list.get(position).getUse_time());

            String strYes = list.get(position).getCorrect_cnt();
            String strWrong = list.get(position).getError_cnt();
            String strLeak = list.get(position).getMissing_cnt();
            yes = Integer.parseInt(strYes);
            Wrong = Integer.parseInt(strWrong);
            Leak = Integer.parseInt(strLeak);
            int Total = yes + Wrong + Leak;
            houder.tvScollTime.setText(TimeFormat.DateFormat(list.get(position).getCreate_time()));
            houder.tvScollCount.setText(list.get(position).getUnit_name());
            houder.tvScollYes.setText("对:" + yes + "/" + Total + "");
            houder.tvScollWrong.setText("错:" + Wrong + "/" + Total);
            houder.tvScollLeak.setText("漏:" + Leak + "/" + Total);
            houder.tvScollData.setText(TimeFormat.formatTime(time * 1000));
            Log.d("tvScollWrong", Leak + "\t\t" + Total);
        } catch (Exception e) {
        }

        return convertView;
    }

    public class ViewHouder {
        TextView tvScollTime;
        TextView tvScollCount;//内容
        TextView tvScollYes;//对
        TextView tvScollWrong;//错
        TextView tvScollLeak;//漏
        TextView tvScollData;//用时
    }
}
